<template>
  <el-dialog   
             :model-value="dialogVisible"
             :title="curId ? '编辑名称' : '新增名称'"
             width="700px"
             :before-close="close">
    <el-form ref="ruleForm"
             label-position="left"
             :model="ruleForm"
             status-icon
             :rules="rules"
             label-width="100px"
             class="demo-ruleForm">
      <el-form-item label="国家"
                    prop="country">
        <el-input v-model="ruleForm.country"
                  clearable
                  autocomplete="off" />
      </el-form-item>

      <el-form-item label="城市"
                    prop="city">
        <el-input v-model="ruleForm.city"
                  clearable
                  autocomplete="off" />
      </el-form-item>
      
      <el-form-item label="区域"
                    prop="region">
        <el-input v-model="ruleForm.region"
                  clearable
                  autocomplete="off" />
      </el-form-item>
      

      <el-form-item label="描述"
                    prop="desc">
        <el-input v-model="ruleForm.desc"
                  clearable
                  autocomplete="off" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary"
                   @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import {
  createRegion,
  getRegion,
  updateRegion,
} from "@/api/cloudPlatform/region";

export default {
  name: "CuForm",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    curId: {
      type: Number,
      default: null,
    },
  },

  data () {
    return {
      ruleForm: {
        name: "",
        desc: "",
        accesskey: "",
        secret: "",
      },
      rules: {
        name: [{ required: true, trigger: "blur", message: "名称不能为空" }],
      },
    };
  },
  watch: {
    dialogVisible (v) {
     console.log('监听到子组件开启')
      if (v) {
        if (this.curId) {
          console.log('传入cuId:',this.curId)
          getRegion(this.curId).then((response) => {
            this.ruleForm = response.data;
            console.log("getDate:",response.data)
          });
        }
      }
    },
  },
  methods: {


    close () {
      this.$refs.ruleForm.resetFields();
      this.$emit("close");
    },
    search () {
      this.close();
      this.$emit("search");
    },

    // 提交表单
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.curId) {
            if (!this.ruleForm.id) {
              this.ruleForm.id = null;
            }
            updateRegion(this.curId, this.ruleForm).then((response) => {
              response.data
              this.$message({
                message: "修改成功",
                type: "success",
              });
              this.search();
            });
          } else {
            createRegion(this.ruleForm).then((response) => {
                response.data
              this.$message({
                message: "新增成功",
                type: "success",
              });
              this.search();
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
